﻿using Production_Data_to_Sldmp.ppdEntity;
using Production_Data_to_Sldmp.sldmpEntity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Production_Data_to_Sldmp
{
    public partial class MainForm : Form
    {
        private static readonly string CONNStr = @"server={0};uid={1};pwd={2};database={3}";
        private List<string> ErrorTableList = new List<string>();
        private SqlSugarClient newDb;
        private SqlSugarClient oldDb;
        private List<string> selTable = new List<string>();
        private List<string> paramTable = new List<string>();
        private long SumCount = 0;
        private double SumTimes = 0;
        private string dtFormat = "yyyy/MM/dd";

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnConn_Click(object sender, EventArgs e)
        {
            if (tbOldServerIp.Text == "")
            {
                MessageBox.Show("源服务器ip不能为空。");
                return;
            }
            if (tbOldUser.Text == "")
            {
                MessageBox.Show("源服务器用户名不能为空。");
                return;
            }
            if (tbOldPwd.Text == "")
            {
                MessageBox.Show("源服务器密码不能为空。");
                return;
            }
            if (cbbOldDB.Text == "")
            {
                MessageBox.Show("源数据库名称不能为空。");
                return;
            }
            if (tbNewServerIp.Text == "")
            {
                MessageBox.Show("目的服务器ip不能为空。");
                return;
            }
            if (tbNewUser.Text == "")
            {
                MessageBox.Show("目的服务器用户名不能为空。");
                return;
            }
            if (tbNewPwd.Text == "")
            {
                MessageBox.Show("目的服务器密码不能为空。");
                return;
            }
            if (cbbNewDB.Text == "")
            {
                MessageBox.Show("目的数据库名称不能为空。");
                return;
            }

            try
            {
                // 连接源数据库
                oldDb = new SqlSugarClient(new ConnectionConfig
                {
                    ConnectionString = string.Format(CONNStr, tbOldServerIp.Text, tbOldUser.Text, tbOldPwd.Text, cbbOldDB.Text),
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true,
                }, db =>
                {
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
                    };
                });
                newDb = new SqlSugarClient(new ConnectionConfig
                {
                    ConnectionString = string.Format(CONNStr, tbNewServerIp.Text, tbNewUser.Text, tbNewPwd.Text, cbbNewDB.Text),
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true,
                }, db =>
                {
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
                    };
                });
                if (!oldDb.Ado.IsValidConnection())
                {
                    MessageBox.Show("源数据库连接失败，请检查配置");
                    return;
                }
                if (!newDb.Ado.IsValidConnection())
                {
                    MessageBox.Show("目标数据库连接失败，请检查配置");
                    return;
                }
                MessageBox.Show("数据库连接成功。", "提示框");
                groupBox4.Enabled = true;
                panel1.Enabled = true;
                btnDisConn.Enabled = true;
                btnConn.Enabled = false;
                groupBox2.Enabled = false;
                groupBox3.Enabled = false;
            }
            catch (Exception ex)
            {
                // 抛出异常
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDisConn_Click(object sender, EventArgs e)
        {
            oldDb?.Close();
            newDb?.Close();
            groupBox4.Enabled = false;
            panel1.Enabled = false;
            btnConn.Enabled = true;
            btnDisConn.Enabled = false;
            groupBox2.Enabled = true;
            groupBox3.Enabled = true;
        }

        private void dtpStart_ValueChanged(object sender, EventArgs e)
        {
            if (dtpStart.Value > dtpEnd.Value)
            {
                dtpEnd.Value = dtpStart.Value;
            }
        }

        private void dtpEnd_ValueChanged(object sender, EventArgs e)
        {
            if (dtpStart.Value > dtpEnd.Value)
            {
                dtpStart.Value = dtpEnd.Value;
            }
        }

        private void cbbSelDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbbSelDate.SelectedIndex)
            {
                case 1:
                    dtpStart.Value = dtpEnd.Value.AddDays(-6);
                    break;

                case 2:
                    dtpStart.Value = dtpEnd.Value.AddDays(-14);
                    break;

                case 3:
                    dtpStart.Value = dtpEnd.Value.AddMonths(-1);
                    break;

                case 4:
                    dtpStart.Value = dtpEnd.Value.AddMonths(-3);
                    break;

                case 5:
                    dtpStart.Value = dtpEnd.Value.AddMonths(-6);
                    break;

                case 6:
                    dtpStart.Value = dtpEnd.Value.AddYears(-1);
                    break;

                case 7:
                    dtpStart.Value = dtpEnd.Value.AddYears(-2);
                    break;

                case 8:
                    dtpStart.Value = dtpEnd.Value.AddYears(-3);
                    break;

                default:
                    dtpStart.Value = DateTime.Today;
                    break;
            }
        }

        private void ccbSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbSelectAll.Checked)
            {
                // 生产数据
                ccbMainEQState.Checked = true;
                ccbDispatchLog.Checked = true;
                ccbAttendance.Checked = true;
                ccbYieldDJ.Checked = true;
                ccbMaterialAvg.Checked = true;
                ccbStockDJ.Checked = true;
                ccbProductParam.Checked = true;
                ccbAuxiliaryEC.Checked = true;
                ccbCastingProduction.Checked = true;
                ccbStorageTrans.Checked = true;
                ccbPower.Checked = true;
                ccbPower10kV.Checked = true;
                ccbQualityTestInfo.Checked = true;
                ccbStock_YHLInfo.Checked = true;
                ccbStock_YJTKInfo.Checked = true;
            }
            else
            {
                // 生产数据
                ccbMainEQState.Checked = false;
                ccbDispatchLog.Checked = false;
                ccbAttendance.Checked = false;
                ccbYieldDJ.Checked = false;
                ccbMaterialAvg.Checked = false;
                ccbStockDJ.Checked = false;
                ccbProductParam.Checked = false;
                ccbAuxiliaryEC.Checked = false;
                ccbCastingProduction.Checked = false;
                ccbStorageTrans.Checked = false;
                ccbPower.Checked = false;
                ccbPower10kV.Checked = false;
                ccbQualityTestInfo.Checked = false;
                ccbStock_YHLInfo.Checked = false;
                ccbStock_YJTKInfo.Checked = false;
            }
        }

        private void ccbAttendance_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbAttendance.Checked)
            {
                selTable.Add(TableConstant.Pos_Attendance);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_Attendance);
            }
        }

        private void ccbMainEQState_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbMainEQState.Checked)
            {
                selTable.Add(TableConstant.Pos_MainEQState);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_MainEQState);
            }
        }

        private void ccbDispatchLog_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbDispatchLog.Checked)
            {
                selTable.Add(TableConstant.Pos_DispatchLog);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_DispatchLog);
            }
        }

        private void ccbYieldDJ_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbYieldDJ.Checked)
            {
                selTable.Add(TableConstant.Pos_YieldDJ);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_YieldDJ);
            }
        }

        private void ccbMaterialAvg_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbMaterialAvg.Checked)
            {
                selTable.Add(TableConstant.Pos_MaterialAvg);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_MaterialAvg);
            }
        }

        private void ccbStockDJ_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbStockDJ.Checked)
            {
                selTable.Add(TableConstant.Pos_StockDJ);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_StockDJ);
            }
        }

        private void ccbProductParam_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbProductParam.Checked)
            {
                selTable.Add(TableConstant.Pos_ProductParam);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_ProductParam);
            }
        }

        private void ccbAuxiliaryEC_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbAuxiliaryEC.Checked)
            {
                selTable.Add(TableConstant.Pos_AuxiliaryEC);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_AuxiliaryEC);
            }
        }

        private void ccbCastingProduction_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbCastingProduction.Checked)
            {
                selTable.Add(TableConstant.Pos_CastingProduction);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_CastingProduction);
            }
        }

        private void ccbStorageTrans_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbStorageTrans.Checked)
            {
                selTable.Add(TableConstant.Pos_StorageTrans);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_StorageTrans);
            }
        }

        private void ccbPower_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbPower.Checked)
            {
                selTable.Add(TableConstant.Pos_Power);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_Power);
            }
        }

        private void ccbPower10kV_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbPower10kV.Checked)
            {
                selTable.Add(TableConstant.Pos_Power10kV);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_Power10kV);
            }
        }

        private void ccbQualityTestInfo_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbQualityTestInfo.Checked)
            {
                selTable.Add(TableConstant.Pos_QualityTestInfo);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_QualityTestInfo);
            }
        }

        private void ccbStock_YHLInfo_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbStock_YHLInfo.Checked)
            {
                selTable.Add(TableConstant.Pos_Stock_YHLInfo);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_Stock_YHLInfo);
            }
        }

        private void ccbStock_YJTKInfo_CheckedChanged(object sender, EventArgs e)
        {
            if (ccbStock_YJTKInfo.Checked)
            {
                selTable.Add(TableConstant.Pos_Stock_YJTKInfo);
            }
            else
            {
                selTable.Remove(TableConstant.Pos_Stock_YJTKInfo);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            groupBox4.Enabled = false;
            panel1.Enabled = false;
            btnDisConn.Enabled = false;
            statusLabel.Text = "";
            cbbSelDate.SelectedIndex = 0;
            dtpEnd.Value = DateTime.Today;
            dtpEnd.MaxDate = DateTime.Today;
            dtpStart.Value = DateTime.Today;
            dtpStart.MaxDate = DateTime.Today;
            tbOldServerIp.Text = "192.168.9.50";
            tbOldUser.Text = "pems";
            tbOldPwd.Text = "sun.moon.earth";
            cbbOldDB.SelectedIndex = 0;
            tbNewServerIp.Text = "192.168.1.210";
            tbNewUser.Text = "aepm";
            tbNewPwd.Text = "lde@aepm";
            cbbNewDB.SelectedIndex = 0;
            paramTable.Clear();
            this.Text += " V" + Application.ProductVersion;
        }

        private void btnDataMigration_Click(object sender, EventArgs e)
        {
            if (selTable == null || selTable.Count == 0)
            {
                MessageBox.Show("请先选择要迁移的数据");
                return;
            }

            DialogResult res = MessageBox.Show("数据迁移会删除目标数据库时间范围内的相关表的数据，\r\n确定开始迁移/同步吗？", "确认框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (res != DialogResult.OK)
            {
                return;
            }

            btnDataMigration.Enabled = false;
            groupBox1.Enabled = false;
            groupBox4.Enabled = false;

            // 创建线程
            Task.Factory.StartNew(async () =>
            {
                foreach (string item in selTable)
                {
                    if (item == TableConstant.Pos_MainEQState)
                    {
                        // 迁移主要设备状态数据
                        await MigrateDataAsync("主要设备运行状态");
                    }
                    else if (item == TableConstant.Pos_DispatchLog)
                    {
                        // 迁移生产调度日志数据
                        await MigrateDataAsync("生产调度日志");
                    }
                    else if (item == TableConstant.Pos_Attendance)
                    {
                        // 迁移部门考勤数据
                        await MigrateDataAsync("部门考勤");
                    }
                    else if (item == TableConstant.Pos_YieldDJ)
                    {
                        // 迁移电解产量数据
                        await MigrateDataAsync("电解产量");
                    }
                    else if (item == TableConstant.Pos_MaterialAvg)
                    {
                        // 迁移物料日均消耗数据
                        await MigrateDataAsync("物料日均消耗");
                    }
                    else if (item == TableConstant.Pos_StockDJ)
                    {
                        // 迁移电解物料库存数据
                        await MigrateDataAsync("电解物料库存");
                    }
                    else if (item == TableConstant.Pos_ProductParam)
                    {
                        // 迁移电解生产参数数据
                        await MigrateDataAsync("电解生产参数");
                    }
                    else if (item == TableConstant.Pos_AuxiliaryEC)
                    {
                        // 迁移辅助能源消耗数据
                        await MigrateDataAsync("辅助能源消耗");
                    }
                    else if (item == TableConstant.Pos_CastingProduction)
                    {
                        // 迁移铸造铝产量数据
                        await MigrateDataAsync("铸造铝产量");
                    }
                    else if (item == TableConstant.Pos_StorageTrans)
                    {
                        // 迁移储运原材料出入库数据
                        await MigrateDataAsync("储运原材料出入库");
                    }
                    else if (item == TableConstant.Pos_Power)
                    {
                        // 迁移动力用电数据
                        await MigrateDataAsync("动力用电");
                    }
                    else if (item == TableConstant.Pos_Power10kV)
                    {
                        // 迁移生产10kV用电数据
                        await MigrateDataAsync("生产10kV用电");
                    }
                    else if (item == TableConstant.Pos_QualityTestInfo)
                    {
                        // 迁移原辅料抽检数据
                        await MigrateDataAsync("原辅料抽检");
                    }
                    else if (item == TableConstant.Pos_Stock_YHLInfo)
                    {
                        // 迁移氧化铝生产用量及库存数据
                        await MigrateDataAsync("氧化铝生产用量及库存");
                    }
                    else if (item == TableConstant.Pos_Stock_YJTKInfo)
                    {
                        // 迁移阳极用量及库存数据
                        await MigrateDataAsync("阳极用量及库存");
                    }
                }
                statusLabel.Text = string.Format("迁移完成，共：{0}条数据。耗时：{1}", SumCount, DateTimeUtils.SecondToTime(SumTimes));
                tbMemo.AppendText(statusLabel.Text + "\r\n");
            });
            btnDataMigration.Enabled = true;
            groupBox1.Enabled = true;
            groupBox4.Enabled = true;
        }

        /// <summary>
        /// 迁移生产核心数据
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private async Task MigrateDataAsync(string type)
        {
            try
            {
                int subCount = 0;
                double subTimes = 0;
                DateTime dt2 = dtpEnd.Value;
                DateTime dt1 = dtpStart.Value;
                var dt1Str = dt1.ToString("yyyyMMdd");
                var dt2Str = dt2.ToString("yyyyMMdd");
                // 查询源数据库数据
                if (type == "主要设备运行状态")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1Str + "000";
                    dt2Str = dt2Str + "999";
                    var list = await oldDb.Queryable<Pos_MainEQStateInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertEqStateInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_main_eq_stateInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_main_eq_stateInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "生产调度日志")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    var list = await oldDb.Queryable<Pos_DispatchLogInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertDispatchLogInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_dispatch_logInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_dispatch_logInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "部门考勤")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1Str + "00";
                    dt2Str = dt2Str + "99";
                    var list = await oldDb.Queryable<Pos_AttendanceInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertAttendanceInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_attendanceInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_attendanceInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "电解产量")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "31";
                    var list = await oldDb.Queryable<Pos_YieldDJInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertYieldDjInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_yield_djInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_yield_djInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "物料日均消耗")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "99";
                    var list = await oldDb.Queryable<Pos_MaterialAvgInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertMaterialAvgInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_material_avgInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_material_avgInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "电解物料库存")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "99";
                    var list = await oldDb.Queryable<Pos_StockDJInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertStockDJInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_stock_djInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_stock_djInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "电解生产参数")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "000";
                    dt2Str = dt2.ToString("yyyyMM") + "999";
                    var list = await oldDb.Queryable<Pos_ProductParamInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertProductParamInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_product_paramInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_product_paramInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "辅助能源消耗")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "99";
                    var list = await oldDb.Queryable<Pos_AuxiliaryECInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertAuxiliaryECInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_auxiliary_ecInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_auxiliary_ecInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "铸造铝产量")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "99";
                    var list = await oldDb.Queryable<Pos_CastingProductionInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertCastingProductionInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_casting_productionInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_casting_productionInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "储运原材料出入库")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "99";
                    var list = await oldDb.Queryable<Pos_StorageTransInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertStorageMaterialInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_storage_transInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_storage_transInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "动力用电")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "99";
                    var list = await oldDb.Queryable<Pos_PowerInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertPowerInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_powerInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_powerInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "生产10kV用电")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "99";
                    var list = await oldDb.Queryable<Pos_Power10kVInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertPower10kvEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_power_10kvInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_power_10kvInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "原辅料抽检")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "99";
                    var list = await oldDb.Queryable<Pos_QualityTestInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertQualityTestInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_quality_testInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_quality_testInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "氧化铝生产用量及库存")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "99";
                    var list = await oldDb.Queryable<Pos_Stock_YHLInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertStockYHLInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_stock_aluminaInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_stock_aluminaInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
                else if (type == "阳极用量及库存")
                {
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始查询{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    DateTime tStart = DateTime.Now;
                    dt1Str = dt1.ToString("yyyyMM") + "00";
                    dt2Str = dt2.ToString("yyyyMM") + "99";
                    var list = await oldDb.Queryable<Pos_Stock_YJTKInfo>()
                    .Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ToListAsync();
                    if (list == null || list.Count == 0)
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new Action(() =>
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                            }));
                        }
                        return;
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("开始迁移{0}--{1}的{2}数据...", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    // 开始事务
                    newDb.Ado.BeginTran();
                    // 将老数据映射到新数据
                    var newList = EntityConvertUtil.ConvertStockAnodeCarbonInfoEntity(list);
                    // 删除目标数据库时间范围内的数据
                    newDb.Deleteable<Ppd_stock_anode_carbonInfo>().Where(it => SqlFunc.Between(it.Id, dt1Str, dt2Str)).ExecuteCommand();
                    // 插入新数据
                    int count = (newList.Count < 2000) ?
                        newDb.Insertable(newList).PageSize(200).ExecuteCommand() :
                        newDb.Fastest<Ppd_stock_anode_carbonInfo>().PageSize(2000).BulkCopy(newList);
                    // 提交事务
                    newDb.Ado.CommitTran();
                    // 计算耗时
                    TimeSpan span = DateTime.Now - tStart;
                    double seconds = (double)(Math.Round(span.TotalSeconds * 1000)) / 1000;
                    subTimes += seconds;
                    subCount += count; if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}--{1}的{2}数据迁移完成，迁移{3}条。耗时：{4}秒", dt1.ToString(dtFormat), dt2.ToString(dtFormat), type, count, seconds);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    if (InvokeRequired)
                    {
                        Invoke(new Action(() =>
                        {
                            statusLabel.Text = string.Format("{0}数据迁移完成，累计{1}条数据。耗时：{2}秒", type, subCount, subTimes);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                        }));
                    }
                    SumCount += subCount;
                    SumTimes += subTimes;
                }
            }
            catch (Exception ex)
            {
                if (InvokeRequired)
                {
                    Invoke(new Action(() =>
                    {
                        statusLabel.Text = string.Format("{0}数据迁移失败，正在回滚...", type);
                        tbMemo.AppendText(string.Format("{0}数据迁移失败，原因：{1}\r\n", type, ex.Message));
                    }));
                }
                // 回滚事务
                newDb.Ado.RollbackTran();
            }
        }

        private void btnBaseDataSync_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("基础数据同步会清空目标数据库相关表的数据，\r\n确定开始数据同步吗？", "确认框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (res != DialogResult.OK)
            {
                return;
            }

            btnDataMigration.Enabled = false;
            groupBox1.Enabled = false;
            groupBox4.Enabled = false;

            int count = 0;

            List<string> types = new List<string> { "考勤部门", "主要设备", "产量计划", "生产指标计划", "原辅料库存预警计划" };
            // 遍历数据类型
            foreach (var type in types)
            {
                statusLabel.Text = string.Format("开始读取{0}数据...", type);
                tbMemo.AppendText(statusLabel.Text + "\r\n");
                // 同步数据
                try
                {
                    switch (type)
                    {
                        case "考勤部门":
                            // 查询源数据库数据
                            var list1 = oldDb.Queryable<Pos_BaseAttendDeptInfo>().ToList();
                            if (list1 == null || list1.Count == 0)
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                                break;
                            }
                            // 开始事务
                            newDb.Ado.BeginTran();
                            // 将老数据映射到新数据
                            var newList1 = EntityConvertUtil.ConvertBaseAttendDeptInfoEntity(list1);
                            // 清空目标数据库数据
                            newDb.DbMaintenance.TruncateTable<Ppd_base_attend_deptInfo>();
                            // 插入新数据
                            count = (newList1.Count < 2000) ?
                                newDb.Insertable(newList1).PageSize(200).ExecuteCommand() :
                                newDb.Fastest<Ppd_base_attend_deptInfo>().PageSize(2000).BulkCopy(newList1);
                            break;

                        case "主要设备":
                            // 查询源数据库数据
                            var list2 = oldDb.Queryable<Pos_BaseMainEQInfo>().ToList();
                            if (list2 == null || list2.Count == 0)
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                                return;
                            }
                            statusLabel.Text = string.Format("开始迁移{0}数据...", type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                            // 开始事务
                            newDb.Ado.BeginTran();
                            // 将老数据映射到新数据
                            var newList2 = EntityConvertUtil.ConvertBaseMainEqInfoEntity(list2);
                            // 清空目标数据库数据
                            newDb.DbMaintenance.TruncateTable<Ppd_base_main_eqInfo>();
                            // 插入新数据
                            int count2 = (newList2.Count < 2000) ?
                                newDb.Insertable(newList2).PageSize(200).ExecuteCommand() :
                                newDb.Fastest<Ppd_base_main_eqInfo>().PageSize(2000).BulkCopy(newList2);
                            break;

                        case "产量计划":
                            // 查询源数据库数据
                            var list3 = oldDb.Queryable<Pos_Plan_YieldInfo>().ToList();
                            if (list3 == null || list3.Count == 0)
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                                return;
                            }
                            statusLabel.Text = string.Format("开始迁移{0}数据...", type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                            // 开始事务
                            newDb.Ado.BeginTran();
                            // 将老数据映射到新数据
                            var newList3 = EntityConvertUtil.ConvertPlanYieldInfoEntity(list3);
                            // 清空目标数据库数据
                            newDb.DbMaintenance.TruncateTable<Ppd_plan_yieldInfo>();
                            // 插入新数据
                            int count3 = (newList3.Count < 2000) ?
                                newDb.Insertable(newList3).PageSize(200).ExecuteCommand() :
                                newDb.Fastest<Ppd_plan_yieldInfo>().PageSize(2000).BulkCopy(newList3);
                            break;

                        case "生产指标计划":
                            // 查询源数据库数据
                            var list4 = oldDb.Queryable<Pos_Plan_ParamterInfo>().ToList();
                            if (list4 == null || list4.Count == 0)
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                                return;
                            }
                            statusLabel.Text = string.Format("开始迁移{0}数据...", type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                            // 开始事务
                            newDb.Ado.BeginTran();
                            // 将老数据映射到新数据
                            var newList4 = EntityConvertUtil.ConvertPlanParameterInfoEntity(list4);
                            // 清空目标数据库数据
                            newDb.DbMaintenance.TruncateTable<Ppd_plan_parameterInfo>();
                            // 插入新数据
                            int count4 = (newList4.Count < 2000) ?
                                newDb.Insertable(newList4).PageSize(200).ExecuteCommand() :
                                newDb.Fastest<Ppd_plan_parameterInfo>().PageSize(2000).BulkCopy(newList4);
                            break;

                        case "原辅料库存预警计划":
                            // 查询源数据库数据
                            var list5 = oldDb.Queryable<Pos_Plan_StockInfo>().ToList();
                            if (list5 == null || list5.Count == 0)
                            {
                                tbMemo.AppendText(string.Format("没有{0}数据...\r\n", type));
                                return;
                            }
                            statusLabel.Text = string.Format("开始迁移{0}数据...", type);
                            tbMemo.AppendText(statusLabel.Text + "\r\n");
                            // 开始事务
                            newDb.Ado.BeginTran();
                            // 将老数据映射到新数据
                            var newList5 = EntityConvertUtil.ConvertPlanStockInfoEntity(list5);
                            // 清空目标数据库数据
                            newDb.DbMaintenance.TruncateTable<Ppd_plan_stockInfo>();
                            // 插入新数据
                            int count5 = (newList5.Count < 2000) ?
                                newDb.Insertable(newList5).PageSize(200).ExecuteCommand() :
                                newDb.Fastest<Ppd_plan_stockInfo>().PageSize(2000).BulkCopy(newList5);
                            break;
                    }
                    // 提交事务
                    newDb.Ado.CommitTran();
                    statusLabel.Text = string.Format("{0}数据迁移完成，迁移{1}条。", type, count);
                    tbMemo.AppendText(statusLabel.Text + "\r\n");
                }
                catch (Exception ex)
                {
                    statusLabel.Text = string.Format("{0}数据迁移失败，正在回滚...", type);
                    tbMemo.AppendText(string.Format("{0}数据迁移失败，原因：{1}\r\n", type, ex.Message));
                    // 回滚事务
                    newDb.Ado.RollbackTran();
                }
            }

            btnDataMigration.Enabled = true;
            groupBox1.Enabled = true;
            groupBox4.Enabled = true;
        }
    }
}